package com.zzb.weikeautosign.taskmanage;

import com.zzb.weikeautosign.Constants;
import com.zzb.weikeautosign.RedisUtil;
import com.zzb.weikeautosign.communication.CommuicationUtil;
import com.zzb.weikeautosign.threadpool.ExcuteThread;
import com.zzb.weikeautosign.threadpool.ThreadQueue;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.ArrayUtils;
import org.joda.time.DateTime;
import redis.clients.jedis.Jedis;

import java.util.*;

/**
 * 定时任务执行类
 * Created by zzb on 16/2/19.
 */
public class ExecuteTask extends TimerTask {

    @Override
    public void run() {
        if(ThreadQueue.getInstances().size()!=0){return;}
        Jedis jedis = RedisUtil.getJedis();
        assert jedis != null;
        Map<String,String> freeUserMap =  jedis.hgetAll(Constants.FREEUSER);
        Map<String,String> thirtyDayUserMap = jedis.hgetAll(Constants.THIRYDAYUSER);
        Map<String,String> halfYearUserMap = jedis.hgetAll(Constants.HALFYEARUSER);
        Map<String,String> oneYearUserMap = jedis.hgetAll(Constants.ONEYEARUSER);
        //优先启动年费用户,其次启用半年用户,再次启用月费用户,最次启用免费用户
        Map<String,String>[] userMaps = new Map[]{oneYearUserMap,halfYearUserMap,thirtyDayUserMap,freeUserMap};

        String[] keys = {Constants.ONEYEARUSER,Constants.HALFYEARUSER,Constants.THIRYDAYUSER,Constants.FREEUSER};
        int [] dueday = {356,178,30,7};

        for(int i =0 ; i < userMaps.length ; i++){
            Map<String,String> userMap = userMaps[i];
            if(MapUtils.isNotEmpty(userMaps[i])){
                Set<String> keyset = userMap.keySet();
                for(String s :keyset) {
                    String valueStr = userMap.get(s);
                    String values[] = valueStr.split(",");
                    if(ArrayUtils.isNotEmpty(values) &&
                            values.length == 3){
                        String name = values[0];
                        String pass = values[1];
                        DateTime signUpTime = new DateTime(Long.parseLong(values[2]));
                        DateTime nowDate = DateTime.now();
                        if(nowDate.minusDays(dueday[i]).compareTo(signUpTime) < 0){
                            ThreadQueue.getInstances().add(new ExcuteThread(name,pass));
                        }
                        //如果超期就要提示,并将用户移入超期列表
                        else{
                            CommuicationUtil.push(name,"已经过期了呢,帅哥/美女,赞助点小钱就可以享用了哦~~3元/月,10元/学期,15元/年,30元/永久");
                            jedis.hdel(keys[i],name);
                            if(!jedis.exists(Constants.OVERTIMEUSER)){
                                Map<String,String> overtimeuserMap = new HashMap<>();
                                overtimeuserMap.put(name,name+","+pass+","+nowDate.getMillis());
                                jedis.hmset(Constants.OVERTIMEUSER,overtimeuserMap);
                            }else {
                                jedis.hset(Constants.OVERTIMEUSER,name,name+","+pass+","+nowDate.getMillis());
                            }
                        }
                    }
                }
            }

        }
    }

}
